﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using BLL;
using Model;

namespace ProductionAnalysis.Web
{
    public partial class ViewbyProduct_CustomerCategory : System.Web.UI.Page
    {
        private int count;

        private string var_productTypeID
        {
            get { return ViewState["productTypeID"].ToString(); }
            set { ViewState["productTypeID"] = value; }
        }
        private DateTime dt1
        {
            get { return DateTime.Parse(ViewState["dt1"].ToString()); }
            set { ViewState["dt1"] = value; }
        }
        private DateTime dt2
        {
            get { return DateTime.Parse(ViewState["dt2"].ToString()); }
            set { ViewState["dt2"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindCustoms();
                BindProductTypes();
            }
        }

        protected void BindCustoms()
        {
            Customer cust = new Customer();
            this.DropDownList1.DataSource = cust.LoadEntities();
            this.DropDownList1.DataTextField = "CustomerName";
            this.DropDownList1.DataValueField = "CustomerID";
            this.DropDownList1.DataBind();
        }

        protected void BindProductTypes()
        {
            ProductType pt = new ProductType();
            this.DropDownList2.DataSource = pt.GetProductTypesbyCustomerID(this.DropDownList1.SelectedValue);
            this.DropDownList2.DataTextField = "ProductTypeName";
            this.DropDownList2.DataValueField = "ProductTypeID";
            this.DropDownList2.DataBind();
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindProductTypes();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;
            BindTests();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            dt1 = DateTime.Parse(this.TextBox1.Value.Trim());
            dt2 = DateTime.Parse(this.TextBox2.Value.Trim());
            var_productTypeID = this.DropDownList2.SelectedValue;
            BindTests();
        }

        private void BindTests()
        {
            Test test = new Test();
            IList<TestInfo> list = test.GetTestsbyProductID_DT(var_productTypeID, dt1, dt2);
            count = list.Count;
            GridView1.DataSource = list;
            GridView1.DataBind();
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                if (GridView1.Rows[i].Cells[7].Text == "Fail")
                    GridView1.Rows[i].Cells[7].ForeColor = System.Drawing.Color.Red;
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["productSN"] = GridView1.SelectedDataKey["ProductSN"].ToString();
            Session["testTime"] = Convert.ToDateTime(GridView1.SelectedDataKey["TestTime"]);
            Session["ProductTypeID"] = GridView1.SelectedDataKey["ProductTypeID"].ToString();
            Session["UserID"] = GridView1.SelectedDataKey["UserID"].ToString();
            Session["ENAID"] = GridView1.SelectedDataKey["ENAID"].ToString();
            Session["SpecVersion"] = GridView1.SelectedDataKey["SpecVersion"].ToString();
            Session["IsPass"] = GridView1.SelectedDataKey["IsPass"].ToString();

            Response.Redirect("~/ViewTest/ViewYourself/ViewTestItem.aspx");
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                Label lbl = (Label)e.Row.FindControl("Label6");
                lbl.Text = count.ToString();
            }
        }
}
}